﻿using Adnc.Application.Shared.AliyunAudit;
using Adnc.Application.Shared.Services;
using Adnc.Infra.Helper;
using Adnc.Infra.IRepositories;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using WYT.Sys.Application.Contracts.Dtos.Aliyun;
using WYT.Sys.Application.Contracts.Dtos.Common;
using WYT.Sys.Application.Contracts.Services;
using WYT.Sys.Repository.Entities;

namespace WYT.Sys.Application.Services
{
    public class AliyunAppService : AbstractAppService, IAliyunAppService
    {
        private readonly IEfRepository<SmsLog> _smsLogEfRepository;

        private readonly IOptions<AliyunOptions> _options;

        public AliyunAppService(IEfRepository<SmsLog> smsLogEfRepository
            , IOptions<AliyunOptions> options)
        {
            _smsLogEfRepository = smsLogEfRepository;
            _options = options;
        }

        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public  async Task<AppSrvResult<bool>> SendSms(SendSmsDto input)
        {
            Sms sms = new Sms(_options.Value);
            var res = sms.SendSms(input.Tel, "微云淘", input.Temp, input.TempParam);
            await _smsLogEfRepository.InsertAsync(new SmsLog
            {
                Id = IdGenerater.GetNextId(),
                Code = res.Code,
                Bizld = res.BizId,
                Message = res.Message,
                Requestld = res.RequestId,
                SenData = input.TempParam,
                SendTime = DateTime.Now,
                SenTemp = input.Temp,
                Tel = input.Tel
            });
            return res.Code.Equals("OK");
        }

        /// <summary>
        /// 获取Oss签名
        /// </summary>
        /// <param name="dir"></param>
        /// <returns></returns>
        public AppSrvResult<OssSignDto> GetOssSign(string dir)
        { 
            OSS oss = new OSS(_options.Value);
            var data = oss.OssSign(dir);
            return new OssSignDto() {
                AccessKeyId = data.AccessKeyId,
                Dir = data.Dir,
                Host = data.Host,
                Expire = data.Expire,
                Policy = data.Policy,
                Signature = data.Signature,
            };
        }
    }
}
